package handler

import (
	"github.com/gin-gonic/gin"
	"oh-half-month/model"
	"oh-half-month/pkg"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	tel := c.PostForm("tel")
	data := model.User{
		Username: username,
		Password: pkg.Md5(password),
		Tel:      tel,
	}
	err := model.DB.Where("username = ?", username).Find(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "登录失败",
		})
	}
	token, _ := pkg.TokenHandler("1")
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": token,
	})
}

func List(c *gin.Context) {
	var article []model.Article
	model.DB.Find(&article).Debug()
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": article,
	})
}
